import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import axios from 'axios'
import pinia from './index.js'
import useUserStore from './user.js'

const userStore = useUserStore(pinia)

const useViewStore = defineStore('view', () => {
  const office = ref(0) // 1, 2
  const type = ref('all') // public, private, mine
  const page = ref(1)
  const data = ref([])
  const total = ref(0)
  const isLoading = ref(false)

  function setOffice(value) {
    office.value = value
  }

  function setType(value) {
    type.value = value
  }

  function setPage(value) {
    page.value = value
  }

  const viewSettings = computed(() => `${office.value}.${type.value}.${page.value}`)

  const load = () => {
    isLoading.value = true
    let params = {
      page: page.value,
      office: office.value,
      type: type.value,
      user: userStore.user
    }
    return new Promise((resolve, reject) => {
      axios
        .post('api/load_post.php', params)
        .then((response) => {
          data.value = response.data.result
          total.value = response.data.count
          console.log('loadPost', params, response.data)
          isLoading.value = false
          resolve(response.data)
        })
        .catch((error) => {
          console.error(error)
          isLoading.value = false
          reject(error)
        })
    })
  }

  const post = (params) => {
    return new Promise((resolve, reject) => {
      axios
        .post('api/send_post.php', params)
        .then((response) => {
          // ElMessageBox(JSON.stringify(response.data), 'response')
          isLoading.value = false
          console.log('post', response.data)
          resolve(response.data)
          // load().then(() => {
          //   isLoading.value = false
          //   resolve(response.data)
          // })
        })
        .catch((error) => {
          isLoading.value = false
          console.error('post', error)
          reject(error)
        })
    })
  }

  const reply = (params) => {
    isLoading.value = true
    return new Promise((resolve, reject) => {
      axios
        .post('api/send_post.php', params)
        .then((response) => {
          console.log('reply', response.data)
          isLoading.value = false
          resolve(response.data)
          // load().then(() => {
          //   isLoading.value = false
          //   resolve(response.data)
          // })
        })
        .catch((error) => {
          console.error('reply', error)
          isLoading.value = false
          reject(error)
        })
    })
  }

  const operate = (params) => {
    return new Promise((resolve, reject) => {
      axios
        .post('api/update_post.php', params)
        .then((response) => {
          if (response.data.success) {
            //
          } else {
            //
          }
          console.log('operate', response.data)
          resolve(response.data)
        })
        .catch((error) => {
          console.error('operate', error)
          reject(error)
        })
    })
  }

  return {
    office,
    type,
    page,
    data,
    total,
    setOffice,
    setType,
    setPage,
    viewSettings,
    load,
    reply,
    post,
    operate,
    isLoading
  }
})

export default useViewStore
